package it.polimi.travelDream.web.beans;

import it.polimi.it.TravelDream.ejb.PackageMgr;
import it.polimi.it.TravelDream.ejb.UserMgr;
import it.polimi.it.TravelDream.ejb.eaos.ExcursionDTO;
import it.polimi.it.TravelDream.ejb.eaos.FlightDTO;
import it.polimi.it.TravelDream.ejb.eaos.HotelDTO;
import it.polimi.it.TravelDream.ejb.eaos.PackageDTO;
import it.polimi.it.TravelDream.ejb.eaos.PersPackageMgr;
import it.polimi.it.TravelDream.ejb.eaos.PersonalizePackageDTO;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.ToggleEvent;

/**
 * Session Bean implementation class CreationPredefinedPackageBean
 */
@Stateful
@ManagedBean
@RequestScoped
public class CreationPredefinedPackageBean {
	@EJB
	private PackageMgr  packageMgr;
	
	@EJB
	private UserMgr userMgr;
	
	private  List<PackageDTO> packList;
	
   private FlightDTO flightSelected;
	
	private HotelDTO hotelSelected;
	
	private ExcursionDTO excursionSelected;

    private PackageDTO selectedPackage;
	
	
	public FlightDTO getFlightSelected() {
		return flightSelected;
	}

	
	public List<PackageDTO> getPackList() {
		return packList;
	}


	public void setPackList(List<PackageDTO> packList) {
		this.packList = packList;
	}


	public PackageDTO getSelectedPackage() {
		return selectedPackage;
	}


	public void setSelectedPackage(PackageDTO selectedPackage) {
		this.selectedPackage = selectedPackage;
	}


	public void setFlightSelected(FlightDTO flightSelected) {
		this.flightSelected = flightSelected;
	}

	public HotelDTO getHotelSelected() {
		return hotelSelected;
	}

	public void setHotelSelected(HotelDTO hotelSelected) {
		this.hotelSelected = hotelSelected;
	}

	public ExcursionDTO getExcursionSelected() {
		return excursionSelected;
	}

	public void setExcursionSelected(ExcursionDTO excursionSelected) {
		this.excursionSelected = excursionSelected;
	}

	
	// prende la lista di pacchetti personalizzati dello user
	@PostConstruct
	public void getPackageList() {
		packList = new ArrayList<PackageDTO>();
		String ciao = "ciao";
		packList = packageMgr.search(ciao);
		
		//System.out.println("ciao ra **************");
		//System.out.println(persPackList.get(0).getIdbasicproduct1());
	}

	// prende il pacchetto da modificare e cambia il volo
	public void changeFlight(){
		//System.out.println(selectedPersonalizedPackage.getIdbasicproduct1());
		System.out.println(flightSelected.getIdbasicproduct());
		selectedPackage.setIdBasicProduct1(flightSelected.getIdbasicproduct());
		
		saveChanges();
	}
	//cambia l hotel
	public void changeHotel(){
		selectedPackage.setIdBasicProduct2(hotelSelected.getIdbasicproduct());
		
	}

	// cambia l'escursione
	public void changeExcursion(){
		selectedPackage.setIdBasicProduct3(excursionSelected.getIdbasicproduct());
		}

	// salva le modifiche al pacchetto all'interno del database
	public void saveChanges(){
	packageMgr.changeBasicProduct(selectedPackage, userMgr.getUserDTO().getEmail());
		
	}
	
	public void createPackage(){
		packageMgr.createNewPackage();
		
	}
	
	
	
	
/*	
 * 	
	// svuota la lista
	public void clearList(){
		PackageList.clear();
		
	}
	*/	
	 public void onRowToggle(ToggleEvent event) {  
	        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,  
	                                            "Row State " + event.getVisibility(),  
	                                            "IDPackage:" + ((PersonalizePackageDTO) event.getData()).getIdpackage());  
	          
	        FacesContext.getCurrentInstance().addMessage(null, msg);  
	    }  


	
    /**
     * Default constructor. 
     */
    public CreationPredefinedPackageBean() {
        // TODO Auto-generated constructor stub
    }

}
